package zookeeper;

public class OrderService implements Runnable {
	
	private OrderNoGenerator orderNoGenerator = new OrderNoGenerator(); // 定义成全局的订单号生成器
    private ExtLock lock = new ZookeeperDistributedLock(); // zk分布式锁

	@Override
	public synchronized void run() {
		try {
            lock.getLock();
            String orderNo = orderNoGenerator.getOrderNo();
            System.out.println(Thread.currentThread().getName() + "创建了 orderNo：" + orderNo);
        } catch (Exception e) {
        	e.printStackTrace();
        } finally {
            lock.unLock();
        }
	}

	// 测试入口：测试之前，先启动zk服务端
	public static void main(String[] args) {
		for (int i=1; i<=50; i++) { // 开启100个线程，模拟分布式锁的场景
            new Thread(new OrderService()).start();
        }
	}
	
}
